package com.main.common.utils;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.main.common.enums.SaTokenKey;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;

/**
 * SaToken工具類
 */
@Slf4j
@Getter
@Service
public class SaTokenUtil {
    private SaSession session;

    public void Login(String id , String device){
        StpUtil.login(id, device);
    }
    public void setSession(){
        log.info(StpUtil.getTokenValue());
        if(StpUtil.getTokenValue()!=null){
            session = StpUtil.getSession();
        }
    }
    public <T> T getBySessionKey(SaTokenKey saTokenKey,Class<T> tClass){
        if (this.getSession() != null) {
            if (tClass.isInstance(this.getSession().get(String.valueOf(saTokenKey)))) {
                return tClass.cast(this.getSession().get(String.valueOf(saTokenKey)));
            }
            log.info("类型转换失败：{}", this.getSession().get(String.valueOf(saTokenKey)));
        }
        return null;
    }

    public <T> void setBySessionKey(SaTokenKey saTokenKey, T value){
        this.getSession().set(String.valueOf(saTokenKey),value);
    }

}
